package cn.tedu.day08;

import java.util.Arrays;

/**
 * 分析并指定合适的锁对象，使得下面的代码成立
 */
public class Test04 {
    public static void main(String[] args) throws InterruptedException {
        Goo goo = new Goo();
        Thread t1 = new Thread() {
            public void run() {
//				synchronized (?) {
                for(int i=0;i<100;i++) {
                    goo.array = Arrays.copyOf(goo.array, goo.array.length+1);
                    goo.array[goo.array.length-1]=i;
                }
//				}
            }
        };
        Thread t2 = new Thread() {
            public void run() {
//				synchronized (?) {
                for(int i=100;i<200;i++) {
                    goo.array = Arrays.copyOf(goo.array, goo.array.length+1);
                    goo.array[goo.array.length-1]=i;
                }
//				}
            }
        };
        t1.start();
        t2.start();

        Thread.sleep(1000);//阻塞1秒是等待上面两个线程干完活
        System.out.println(goo.array.length);
        System.out.println(Arrays.toString(goo.array));
    }
}

class Goo{
    public int[] array = {};
}